﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchEdu.Framework.Algorithm;
using SchEdu.Framework.DataModel;
using SchEdu.Framework.Objective;


namespace SchEduApp
{
    public partial class AlgorithmForm : Form
    {
        public AlgorithmForm()
        {
            InitializeComponent();
            this.algorithmListBindingSource.DataSource = SchEduAppContext.AlgorithmList; 
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            IAlgorithm algorithm = this.algorithmListBindingSource.Current as IAlgorithm;
            if (algorithm == null)
            {
                return;
            }

            SchEduDataSet.SolutionRow solution;
            try
            {
                solution = RunAlgorithm(algorithm);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SchEdu Algorithm Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                RunObjectives(solution);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SchEdu Objective Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            GanttForm ganttForm = new GanttForm();
            ganttForm.Solution = solution;
            ganttForm.MdiParent = this.MdiParent;
            ganttForm.Show();


        }

        private SchEduDataSet.SolutionRow RunAlgorithm(IAlgorithm algorithm)
        {
            algorithm.SchEduData = SchEduAppContext.SchEduData;
            return algorithm.Run();
        }

        private void RunObjectives(SchEduDataSet.SolutionRow solution)
        {
            Performancer performancer = new Performancer(SchEduAppContext.SchEduData, solution, SchEduAppContext.ObjectiveList);
            performancer.Generate();
        }


    }
}
